﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Phone.Controls;


namespace HappyFrog.NavigateService
{
    public class UriList
    {
        #region Property
        /// <summary>
        /// All the page uris
        /// </summary>
        private List<UriItem> PageUriList { get; set; }

        /// <summary>
        /// Instance of UriList
        /// </summary>
        private static UriList Current = null;
        #endregion

        #region private methods
        /// <summary>
        /// Constructor
        /// </summary>
        private UriList()
        {
            Initialize();
        }

        /// <summary>
        /// Initialize the list
        /// </summary>
        private void Initialize()
        {
            //load all xml via LINQ to XML
            XDocument doc = XDocument.Load("./Resource/XML/PageUri.xml");
            var uris = from uri in doc.Root.Elements("PageUri")
                       select new UriItem
                                {
                                    Name = uri.Attribute("Name").Value,
                                    Adress = uri.Element("Adress").Value
                                };
            this.PageUriList = uris.ToList<UriItem>();
        }
        #endregion

        #region public method
        /// <summary>
        /// Get instance of UriList
        /// </summary>
        /// <returns></returns>
        public static UriList GetInstance()
        {
            if (Current == null)
            {
                Current = new UriList();
            }
            return Current;
        }

        /// <summary>
        /// Navigate to a new page
        /// </summary>
        /// <param name="uri">File name of XAML page</param>
        public Uri GetPageUri(string fileName)
        {
            var uriItem = from anUri in PageUriList where anUri.Name == fileName select anUri;
            if (uriItem.Count<UriItem>() != 1)
                return null;
            return new Uri(uriItem.First<UriItem>().Adress, UriKind.Relative);
        }
        #endregion
    }
}
